const express = require('express')

const db = require('../db')

const md5=require('md5-node')

const router =express.Router();

// --------------------------------------获取用户信息--------------------------------

router.get('/userInfo',(req,res)=>{
    const {user_id}=req.user
    db(`select user_id,phone,nickname,sex,age,avatar,profile from user where user_id='${user_id}'`,(err,data)=>{
        if (err) {
           throw err; 
        }
        res.send({
            code:1,
            msg:'获取成功！',
            data:data
        })
    })
})

// --------------------------------------修改用户信息--------------------------------
router.post('/updInfo',(req,res)=>{
    const {user_id}=req.user
    const {nickname,sex,age,avatar,profile} = req.body;
    let sql=`update user set `
    if (age) {
        sql+=`age=${age},` 
     }
     if (avatar) {
        sql+=`avatar='${avatar}',` 
     }
    sql+=`nickname='${nickname}',sex=${sex},profile='${profile}' where user_id='${user_id}'` 
     db(sql,(err,data)=>{
         if (err) {
             throw err
         }
         res.send({
             code:1,
             msg:'修改成功！'
         })
     })
})

//---------------------------------------修改密码------------------------------------

router.post('/reset_psd',(req,res)=>{
    const {user_id}=req.user
    let {password,newPsd}=req.body;
    password=md5(password)
    newPsd=md5(newPsd)
    db(`select password from user where user_id='${user_id}'`,(err,data)=>{
        if (err) {
            throw err;
        }
        if (data[0].password==password) {
            db(`update user set password='${newPsd}' where user_id='${user_id}'`,(err,data)=>{
                if (err) {
                    throw err;
                }
                console.log(data);
                res.send({
                    code:1,
                    msg:'修改密码成功！' 
                })

            })
        }else{
            res.send({
                code:0,
                msg:'密码错误！'
            })
        }
    })
})

// ----------------------------------------修改手机号码--------------------------------------------
router.post('/upd_phone',(req,res)=>{
    const {user_id}=req.user
    const {newPhone,code}=req.body
    if (code=='442211') {
        db(`select * from user where phone='${newPhone}'`,(err,data)=>{
            if (err) {
                throw err;
            }
            if (data.length) {
                res.send({
                    code:0,
                    msg:'该手机号码已被注册！'
                })
            }else{
                db(`update user set phone='${newPhone}' where user_id='${user_id}'`,(err,data)=>{
                    if (err) {
                        throw err;
                    }
                    res.send({
                        code:1,
                        msg:'修改手机号码成功！' 
                    })
        
                })
            }
        })
        
    }else{
        res.send({
            code:0,
            msg:'验证码错误！' 
        })
    }
})

// --------------------------------------获取所有用户信息--------------------------------
router.get('/get_all_user',(req,res)=>{
    db(`select * from user`,(err,data)=>{
        if (err) {
           throw err; 
        }
        res.send({
            code:1,
            msg:'获取成功！',
            data:data
        })
    })
})

module.exports=router